package cn.com.edu.nju.ecrec.rec;

import java.sql.*;

import cn.com.edu.nju.ecrec.model.UserInfo;

public class Bayesian {
	private UserInfo ui;
	
	public Bayesian(UserInfo ui){
		this.ui = ui;
	}
	
	public double Rate(long pid) {
		double result = 0;
		try {
			String driverName = "com.mysql.jdbc.Driver";
			String userName = "root";
			String userPasswd = "123456";
			String url = "jdbc:mysql://localhost:3307/ECRec";
			Class.forName(driverName).newInstance();
			Connection con = DriverManager.getConnection(url, userName,
					userPasswd);
			Statement statement = con.createStatement();

			String sql1 = "select count(*) from userinfo";
			ResultSet rs = statement.executeQuery(sql1);
			rs.next();
			int userCount = rs.getInt(1);
			System.out.println("userCount:" + userCount);

			String sql2 = "select count(*) from useritem where pid=" + pid;
			rs = statement.executeQuery(sql2);
			rs.next();
			int buyCount = rs.getInt(1);
			buyCount += 1;
			System.out.println("buyCount:" + buyCount);

			String sql3 = "select count(*) from useritem where pid="
					+ pid
					+ " and infoid in (select infoid from userinfo where browser='"
					+ ui.getBrowser() + "')";
			rs = statement.executeQuery(sql3);
			rs.next();
			int browserCount = rs.getInt(1);
			browserCount += 1;
			buyCount += 1;

			String sql4 = "select count(*) from useritem where pid="
					+ pid
					+ " and infoid in (select infoid from userinfo where country='"
					+ ui.getCountry() + "')";
			rs = statement.executeQuery(sql4);
			rs.next();
			int countryCount = rs.getInt(1);
			countryCount += 1;
			buyCount += 1;

			String sql5 = "select count(*) from useritem where pid="
					+ pid
					+ " and infoid in (select infoid from userinfo where platform='"
					+ ui.getPlatform() + "')";
			rs = statement.executeQuery(sql5);
			rs.next();
			int platformCount = rs.getInt(1);
			platformCount += 1;
			buyCount += 1;

			String sql6 = "select count(*) from useritem where pid="
					+ pid
					+ " and infoid in (select infoid from userinfo where userLanguage='"
					+ ui.getUserLanguage() + "')";
			rs = statement.executeQuery(sql6);
			rs.next();
			int userLanCount = rs.getInt(1);
			userLanCount += 1;
			buyCount += 1;

			result = ((double) browserCount / buyCount)
					* ((double) countryCount / buyCount)
					* ((double) platformCount / buyCount)
					* ((double) userLanCount / buyCount)
					* ((double) buyCount / userCount);
			
			rs.close();
			statement.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public long[] recommand(){
		long[] result = new long[5];
		double[] rates = {0,0,0,0,0};
		try {
			String driverName = "com.mysql.jdbc.Driver";
			String userName = "root";
			String userPasswd = "123456";
			String url = "jdbc:mysql://localhost:3307/ECRec";
			Class.forName(driverName).newInstance();
			Connection con = DriverManager.getConnection(url, userName,
					userPasswd);
			Statement statement = con.createStatement();
			
			String sql = "select distinct pid from useritem";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()){
				System.out.println("rowNo. "+rs.getRow());
				long pid = rs.getLong(1);
				double rate = Rate(pid);
				int index = -1;
				for(int i = 0;i < 5;i ++){
					if(rate > rates[i]){
						index = i;
						break;
					}
				}
				if(index != -1){
					if(index != 4){
						int i = 3;
						while(i >= index){
							result[i+1] = result[i];
							rates[i+1] = result[i];
							i--;
						}
					}
					result[index] = pid;
					rates[index] = rate;
				}
			}
			
			rs.close();
			statement.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}